import math
class Calculator:
    from typing import List, Tuple, Dict

    def calculate(self, depth, inner_diameter, top_elevation, ground_elevation, concrete_weight, segments, sections):
        if depth <= 0:
            raise ValueError("井深度必须为正数")
        if inner_diameter <= 0:
            raise ValueError("内径必须为正数")
        # 计算井底标高
        bottom_elevation = top_elevation - depth
        
        # 计算各段信息
        current_elevation = bottom_elevation
        segment_results = []
        for i, seg in enumerate(segments):
            seg["bottom_elevation"] = current_elevation
            seg["top_elevation"] = current_elevation + seg["length"]
            seg["outer_diameter"] = inner_diameter + 2 * seg["thickness"]
            
            # 计算体积
            outer_radius = seg["outer_diameter"] / 2
            inner_radius = inner_diameter / 2
            seg["volume"] = math.pi * seg["length"] * (outer_radius**2 - inner_radius**2)
            
            seg["weight"] = seg["volume"] * concrete_weight
            seg["outer_surface"] = math.pi * seg["outer_diameter"] * seg["length"]
            seg["projection_area"] = math.pi * (seg["outer_diameter"] / 2) ** 2
            
            current_elevation = seg["top_elevation"]
            segment_results.append((
                f"第{i+1}段", 
                f"{seg['bottom_elevation']:.3f}",
                f"{seg['top_elevation']:.3f}",
                f"{seg['thickness']:.3f}",
                f"{seg['outer_diameter']:.3f}",
                f"{seg['weight']:.2f}",
                f"{seg['outer_surface']:.2f}",
                f"{seg['projection_area']:.2f}"
            ))
        
        # 计算各节信息
        current_elevation = bottom_elevation
        section_results = []
        for i, sec in enumerate(sections):
            sec["bottom_elevation"] = current_elevation
            sec["top_elevation"] = current_elevation + sec["length"]
            
            # 确定节所在的段及其参数
            volume = 0
            outer_surface = 0
            projection_area = 0
            
            for seg in segments:
                # 检查节与段是否有重叠
                overlap_bottom = max(sec["bottom_elevation"], seg["bottom_elevation"])
                overlap_top = min(sec["top_elevation"], seg["top_elevation"])
                overlap_length = max(0, overlap_top - overlap_bottom)
                
                if overlap_length > 0:
                    outer_diameter = seg["outer_diameter"]
                    
                    # 计算重叠部分的体积和表面积
                    outer_radius = outer_diameter / 2
                    inner_radius = inner_diameter / 2
                    seg_volume = math.pi * overlap_length * (outer_radius**2 - inner_radius**2)
                    seg_surface = math.pi * outer_diameter * overlap_length
                    
                    volume += seg_volume
                    outer_surface += seg_surface
                    
                    # 投影面积取节底部所在段的值
                    if sec["bottom_elevation"] >= seg["bottom_elevation"] and sec["bottom_elevation"] < seg["top_elevation"]:
                        projection_area = math.pi * (outer_diameter / 2) ** 2
            
            sec["weight"] = volume * concrete_weight
            sec["outer_surface"] = outer_surface
            sec["projection_area"] = projection_area
            
            current_elevation = sec["top_elevation"]
            section_results.append((
                f"第{i+1}节", 
                f"{sec['bottom_elevation']:.3f}",
                f"{sec['top_elevation']:.3f}",
                f"{sec['weight']:.2f}",
                f"{sec['outer_surface']:.2f}",
                f"{sec['projection_area']:.2f}"
            ))
        
        return segment_results, section_results, bottom_elevation